#include "lan2calhead.h"
#include "lan2calcombine.h"
#include "lan2calline.h"

#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
LAN2STA
Lan2CalCombine::Lan2CalCombine(int width, int head_height, int cal_height, int time_height, int control_height, int line_height, QWidget *parent) : QWidget(parent)
{

    this->setFixedWidth(width);

    this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    QVBoxLayout* boxv=new QVBoxLayout();
    boxv->setContentsMargins(10,10,10,10);
    this->setLayout(boxv);

    QWidget* widget=new QWidget(this);
    widget->resize(this->width()-20,this->height()-20);

    QGraphicsDropShadowEffect* shadow=new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0,0);
    shadow->setColor(Qt::black);
    shadow->setBlurRadius(10);
    widget->setGraphicsEffect(shadow);

    vbox=new QVBoxLayout();
     vbox->setSpacing(0);
     vbox->setMargin(0);
     vbox->setContentsMargins(0,0,0,0);
     widget->setLayout(vbox);
     boxv->addWidget(widget);


     mcalhead=new Lan2CalHead(this,width-20,head_height);
     connect(mcalhead,SIGNAL(sign_leftMM()),this,SLOT(slot_leftMM()));
     connect(mcalhead,SIGNAL(sign_leftyyyy()),this,SLOT(slot_leftyyyy()));
     connect(mcalhead,SIGNAL(sign_rightMM()),this,SLOT(slot_rightMM()));
     connect(mcalhead,SIGNAL(sign_rightyyyy()),this,SLOT(slot_rightyyyy()));


     Lan2CalLine* line1=new Lan2CalLine(this,width-20,line_height);




     mcal=new Lan2Cal(this,width-20,cal_height);//日历
     connect(mcal,SIGNAL(currentPageChanged(int,int)),this,SLOT(slot_pagechange(int,int)));
//     mcal->setFixedSize(360,270);

     Lan2CalLine* line2=new Lan2CalLine(this,width-20,line_height);

     mtimewidget=new Lan2CalTime(this,width-20,time_height);//时间
     mtimewidget->method_sethhtxt(QDateTime::currentDateTime().toString("hh"));
     mtimewidget->method_setmmtxt(QDateTime::currentDateTime().toString("mm"));
     mtimewidget->method_setsstxt(QDateTime::currentDateTime().toString("ss"));

//     qDebug()<<topLevelWidget()->x()<<this->x();


     Lan2CalLine* line3=new Lan2CalLine(this,width-20,line_height);

     mcalcontrol=new Lan2CalControl(this,width-20,control_height);//控制按钮
     connect(mcalcontrol,SIGNAL(sign_today()),this,SLOT(slot_today()));
     connect(mcalcontrol,SIGNAL(sign_clear()),this,SLOT(slot_clear()));
     connect(mcalcontrol,SIGNAL(sign_ok()),this,SLOT(slot_ok()));
     connect(mcalcontrol,SIGNAL(sign_tostart()),mtimewidget,SLOT(slot_reset2start()));
     connect(mcalcontrol,SIGNAL(sign_toend()),mtimewidget,SLOT(slot_reset2end()));
//     connect(mcalcontrol,SIGNAL(sign_toall()),this,SLOT(slot_toall()));



     vbox->addWidget(mcalhead);
     vbox->addWidget(line1);
     vbox->addWidget(mcal);
     vbox->addWidget(line2);
     vbox->addWidget(mtimewidget);
     vbox->addWidget(line3);
     vbox->addWidget(mcalcontrol);
     vbox->addStretch();


}

void Lan2CalCombine::method_movehhplane(int x, int y)
{
         mtimewidget->method_movehhplane(x,y);

}

void Lan2CalCombine::method_movemmplane(int x, int y)
{
    mtimewidget->method_movemmplane(x,y);
}

void Lan2CalCombine::method_movessplane(int x, int y)
{
    mtimewidget->method_movessplane(x,y);
}

void Lan2CalCombine::method_delAllplane()
{
    mtimewidget->method_delhhplane();
    mtimewidget->method_delmmplane();
    mtimewidget->method_delssplane();
}

void Lan2CalCombine::method_hideAllplane()
{
    mtimewidget->method_hideAllPlane();
}

void Lan2CalCombine::method_initShadow()
{

}

void Lan2CalCombine::slot_leftMM()
{
    mcal->showPreviousMonth();
//    qDebug()<<mcal->showNextMonth()<<mcal->showNextYear()<<" show";


}

void Lan2CalCombine::slot_leftyyyy()
{
    mcal->showPreviousYear();
}

void Lan2CalCombine::slot_rightMM()
{
    mcal->showNextMonth();
}

void Lan2CalCombine::slot_rightyyyy()
{
    mcal->showNextYear();
}

void Lan2CalCombine::slot_today()
{
    //日期和时间选择当下
    mcal->showToday();
    mcal->setSelectedDate(QDate::currentDate());
    mtimewidget->method_sethhmmsstxt(QDateTime::currentDateTime().toString("hh"),QDateTime::currentDateTime().toString("mm"),QDateTime::currentDateTime().toString("ss"));



    emit sign_combinetoday();


//    qDebug()<<"show today";
}

void Lan2CalCombine::slot_clear()
{

    mtimewidget->method_sethhmmsstxt("00","00","00");
    emit sign_combineclear();
}

void Lan2CalCombine::slot_ok()
{
    if(!mtimewidget->mplane_hh->isHidden()){
        mtimewidget->mplane_hh->setHidden(true);
    }
    this->hide();
    QString yyyy=QString("%1").arg(mcal->selectedDate().year());
    QString MM=QString("%1").arg(mcal->selectedDate().month(),2,10,QLatin1Char('0'));
    QString dd=QString("%1").arg(mcal->selectedDate().day(),2,10,QLatin1Char('0'));
    QString hh=mtimewidget->method_gethh();
    QString mm=mtimewidget->method_getmm();
    QString ss=mtimewidget->method_getss();
    QDateTime dt=QDateTime::fromString(QString("%1-%2-%3 %4:%5:%6")
                                       .arg(yyyy)
                                       .arg(MM)
                                       .arg(dd)
                                       .arg(hh)
                                       .arg(mm)
                                       .arg(ss),"yyyy-MM-dd hh:mm:ss");
//    qDebug()<<dt<<"wok";

    emit sign_combineok(dt);


}

void Lan2CalCombine::slot_pagechange(int yyyy, int MM)
{
//    qDebug()<<yyyy<<" show "<<MM;
    mcalhead->method_setyyyyMM(yyyy,MM);

}

void Lan2CalCombine::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(this->rect().adjusted(10,10,-10,-10),QColor("#c0c0c0"));
}


LAN2END
